package com.konke.smartpad_huahao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class DimenUtil {
    public static void autoDimen(String url, String descUrl, double magnification) {
        try {
            File descFile = new File(descUrl);
            if (!descFile.exists()) {
                descFile.mkdir();
            }
            FileWriter writer = new FileWriter(descFile);
            FileReader reader1 = new FileReader(url);
            BufferedReader reader = new BufferedReader(reader1);
            String line = "";
            while ((line = reader.readLine()) != null) {
                if (line.equals("")) {
                    continue;
                }
                if (!line.contains("dimen")) {
                    writer.write(line + "\n");
                    continue;
                }
                int n1 = line.indexOf("\"");
                int n2 = line.indexOf("\"", n1 + 1);
                String name = line.substring(n1 + 1, n2);
                int d1 = line.indexOf(">");
                int d2 = line.lastIndexOf("<");
                String dimen = line.substring(d1 + 1, d2);
                String unit = dimen.contains("sp") ? "sp" : dimen.contains("px") ? "px" : "dp";
                if (unit.equals("px")) {
                    writer.write(line + "\n");
                    continue;
                }
                String size = dimen.replace(unit, "");
                double old = Double.parseDouble(size);
                double newsize = old * magnification;
                String newdimen = "<dimen name=\"" + name + "\">" + newsize + unit + "</dimen>";
                writer.write(newdimen + "\n");
            }
            writer.flush();
            writer.close();
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void autoDimen1(String url, double magnification) {
        try {
            FileReader reader1 = new FileReader(url);
            BufferedReader reader = new BufferedReader(reader1);
            String line = "";
            while ((line = reader.readLine()) != null) {
                if (line.equals("")) {
                    continue;
                }
                if (!line.contains("dimen")) {
                    continue;
                }
                int n1 = line.indexOf("\"");
                int n2 = line.indexOf("\"", n1 + 1);
                String name = line.substring(n1 + 1, n2);
                int d1 = line.indexOf(">");
                int d2 = line.lastIndexOf("<");
                String dimen = line.substring(d1 + 1, d2);
                String unit = dimen.contains("sp") ? "sp" : dimen.contains("px") ? "px" : "dp";
                String size = dimen.replace(unit, "");
                double old = Double.parseDouble(size);
                double newsize = old * magnification;
                if (unit.equals("dp")) {
                    unit = "px";
                }
                String newdimen = "<dimen name=\"" + name + "\">" + newsize + unit + "</dimen>";
                System.out.println(newdimen);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        autoDimen("C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xhdpi\\dimen.xml", "C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values\\dimen.xml", 1.5);
        autoDimen("C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xhdpi\\dimen.xml", "C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xxhdpi\\dimen.xml", 0.75);
        autoDimen("C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xhdpi\\dimen.xml", "C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-hdpi\\dimen.xml", 1.5);
        autoDimen("C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xhdpi\\dimen.xml", "C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-mdpi\\dimen.xml", 2);
        autoDimen("C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-xhdpi\\dimen.xml", "C:\\Users\\86139\\StudioProjects\\SmartPad\\app\\src\\main\\res\\values-ldpi\\dimen.xml", 2);
    }
}
